package com.java.simple.sort;

import java.util.Arrays;

/**
 * @author zhoujunlin
 * @date 2022年08月09日 11:34
 * @desc 希尔排序
 * https://blog.csdn.net/ren9436/article/details/108893447
 */
public class ShellSort {

    public static void main(String[] args) {
        int[] numArr = new int[]{1, 10, 2, 8, 3, 4, 7, 6, 5, 9};
        shellSort(numArr);
        System.out.println(Arrays.toString(numArr));
    }


    private static void shellSort(int[] numArr) {
        // 确定初始步长
        int gap = 1;
        while (gap < numArr.length / 2) {
            gap = gap * 2 + 1;
        }
        // gap = 7
        while (gap >= 1) {
            // 7 3 1
            for (int i = gap; i < numArr.length; i++) {
                for (int j = i; j >= gap; j -= gap) {
                    if (numArr[j - gap] > numArr[j]) {
                        SwapUtil.swap(numArr, j - gap, j);
                    } else {
                        break;
                    }
                }
            }
            gap = gap / 2;
        }
    }

}
